package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IBankAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class IBankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {
    @Resource
    private JwtTool jwtTool;

    /**
     * 新增银行账户信息
     * @param dto 银行账户信息
     * @param request request
     */
    @Override
    public void addBankAccount(BankAccountUpsertReqDTO dto, HttpServletRequest request) {
        // 根据请求头携带的token解析出用户id,下次再次点击账户设置能够根据根据用户id查询到银行账户信息
        String token = request.getHeader("Authorization");
        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);
        // 数据封装
        BankAccount bankAccount = BeanUtils.copyBean(dto, BankAccount.class);
        bankAccount.setId(currentUserInfo.getId());
        bankAccount.setCreateTime(new Date());
        baseMapper.insert(bankAccount);
    }

    /**
     * 获取当前用户银行账号接口
     * @param request request
     * @return BankAccount
     */
    @Override
    public BankAccount getCurrentUserBankAccount(HttpServletRequest request) {
        // 根据请求头携带的token解析出用户信息从而查询当前登录用户的银行账户信息
        String token = request.getHeader("Authorization");
        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);
        // 根据用户id查询银行账户信息
        return baseMapper.selectOne(
                new LambdaQueryWrapper<BankAccount>()
                        .eq(BankAccount::getId, currentUserInfo.getId())
        );
    }
}
